package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Background;
import com.example.chat.mapper.BackgroundMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.BackgroundResult;
import com.example.chat.result.Base;
import com.example.chat.result.ListResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BackgroundService extends ServiceImpl<BackgroundMapper, Background> {
    @Resource
    CheckUtils checkUtils;
    @Value("${global.config.path}")
    String configPath;
    @Value("${global.config.website}")
    String website;
    @Resource
    Global global;

    public ListResult<List<BackgroundResult>> listBackground(Integer userId, String token, int appType, int type, int backStatus, Integer page, Integer limit) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1101");
        if (apiResult.getStatus() != 1)
            return new ListResult<>(apiResult.getStatus(), apiResult.getMsg(), null, 0, 0, 0);
        ListResult<List<BackgroundResult>> result = new ListResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            Page<Background> pages = new Page<>(page, limit);
            IPage<Background> iPages = baseMapper.selectBySearch(pages, appType, type, backStatus);
            List<BackgroundResult> results = new ArrayList<>();
            for (Background background : iPages.getRecords()) {
                BackgroundResult backgroundResult = new BackgroundResult();
                backgroundResult.setId(background.getId());
                backgroundResult.setFilePath(background.getFilePath());
                backgroundResult.setFileFullPath(website + background.getFilePath());
                backgroundResult.setCreateAdmin(background.getCreateAdminName());
                backgroundResult.setCreateTime(Global.longToString(background.getCreateTime()));
                backgroundResult.setType(background.getType());
                backgroundResult.setAppType(background.getAppType());
                backgroundResult.setStatus(background.getStatus());
                backgroundResult.setUrl(background.getUrl());
                results.add(backgroundResult);
            }
            result.setData(results);
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> addBackground(Integer userId, String token, String filePath, String url, Integer type, Integer backStatus, Integer appType) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, filePath, url), userId, token, "1102");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "创建失败";
        try {
            Background background = new Background();
            background.setFilePath(filePath);
            background.setUrl(url);
            background.setAppType(appType);
            background.setType(type);
            background.setStatus(backStatus);
            background.setCreateAdmin(userId);
            background.setCreateTime(System.currentTimeMillis());
            baseMapper.insert(background);
            status = 1;
            msg = "创建成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> editBackground(Integer userId, String token, Integer ids, String filePath, String url, Integer type, Integer backStatus, Integer appType) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, filePath, url), userId, token, "1103");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            Background background = baseMapper.selectById(ids);
            if (background != null) {
                background.setFilePath(filePath);
                background.setUrl(url);
                background.setAppType(appType);
                background.setType(type);
                background.setStatus(backStatus);
                baseMapper.updateById(background);
                status = 1;
                msg = "更新成功";
            } else msg = "背景图不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> deleteBackground(Integer userId, String token, Integer[] ids) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1104");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "删除失败";
        try {
            for (Integer id : ids) {
                Background background = baseMapper.selectById(id);
                if (background == null) {
                    continue;
                }
                baseMapper.deleteById(id);
            }
            msg = "删除成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<List<BackgroundResult>> allBackground(int appType, int type,int status1) {
        ApiResult<List<BackgroundResult>> result = new ApiResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            List<Background> list = baseMapper.selectAllBySearch(appType, type,status1);
            List<BackgroundResult> results = new ArrayList<>();
            for (Background background : list) {
                BackgroundResult backgroundResult = new BackgroundResult();
                backgroundResult.setId(background.getId());
                backgroundResult.setFilePath(background.getFilePath());
                backgroundResult.setFileFullPath(website + background.getFilePath());
                backgroundResult.setCreateAdmin(background.getCreateAdminName());
                backgroundResult.setCreateTime(Global.longToString(background.getCreateTime()));
                backgroundResult.setType(background.getType());
                backgroundResult.setAppType(background.getAppType());
                backgroundResult.setStatus(background.getStatus());
                backgroundResult.setUrl(background.getUrl());
                results.add(backgroundResult);
            }
            result.setData(results);
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
